﻿using DevExpress.XtraEditors;
using DLL;
using SQLDLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace NursingFormSystem
{
    public partial class SetEduDiseases : Form
    {
        public string sql = string.Empty;
        public string DiseGuid = string.Empty;
        public string departid = string.Empty;
        public SetEduDiseases(string Diseasesid, string departmentid)
        {
            InitializeComponent();
            departid = departmentid;
            StartPosition = FormStartPosition.CenterScreen;
            if (null == Diseasesid)
            {
                Text = "新增表单";
                simpleButton3.Hide();
                Width = 287;
            }
            else
            {
                DiseGuid = Diseasesid;
                Text = "表单维护";
                sql = @"SELECT *  FROM [BNNursingDocsSystem_Develop].[dbo].[HealthEduDiseases](NOLOCK) where DiseasesGuid = '" + DiseGuid + "'";
                DataTable Diseases = SqlHelper.ExecuteDataset(sql, CommandType.Text).Tables[0];
                textEdit1.Text = Diseases.Rows[0]["TemplateName"].ToString();
                textEdit2.Text = Diseases.Rows[0]["DiseasesName"].ToString();
                textEdit3.Text = Diseases.Rows[0]["OrderNum"].ToString();
                departid = Diseases.Rows[0]["DepartmentID"].ToString();
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            string departmentname = "";
            if (departid == null) departid = "";
            if (departid.Length > 0)
            {
                string[] temp = Regex.Split(departid, ",", RegexOptions.IgnoreCase);
                for (int i = 0; i < temp.Length; i++)
                {
                    sql = @"SELECT DepartmentName  FROM [NursingInfoSystem].[dbo].[tb_Department](NOLOCK) where DepartmentCode = '" + temp[i] + "'";
                    object temp2 = SqlHelper.ExecuteScalar(CommandType.Text, sql);
                    departmentname += temp2.ToString() + ",";
                }
                departmentname = departmentname.Substring(0, departmentname.Length - 1);
            }
            if (departid.Length == 0)
            {
                departmentname = "";
            }

            if (Text == "新增表单")
            {
                string guid = Guid.NewGuid().ToString();
                sql = @" insert into [BNNursingDocsSystem_Develop].[dbo].[HealthEduDiseases] (DiseasesName,DepartmentID,DepartmentName,DiseasesGuid,TemplateName,OrderNum) values('" + textEdit2.Text + "','" + departid + "','" + departmentname + "','" + guid + "','" + textEdit1.Text + "','" + textEdit3.Text + "')";
                int key = SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
                if (key == 1)
                {
                    XtraMessageBox.Show("新增成功");
                    Close();
                    Hide();
                    SetEduDiseases F4 = new SetEduDiseases(guid, departid);
                    F4.ShowDialog();
                }
            }
            else
            {
                sql = @" update BNNursingDocsSystem_Develop.dbo.[HealthEduDiseases] set DiseasesName ='" + textEdit2.Text + "',DepartmentName='" + departmentname + "',DepartmentID ='" + departid + "',TemplateName='" + textEdit1.Text + "',OrderNum='" + textEdit3.Text + "' where DiseasesGuid='" + DiseGuid + "'";
                int key = SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
                if (key == 1)
                {
                    XtraMessageBox.Show("修改成功");
                    DialogResult = DialogResult.OK;
                    Close();
                }
            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            SetEduClass F5 = new SetEduClass(DiseGuid);
            F5.ShowDialog();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            Close();
            DialogResult = System.Windows.Forms.DialogResult.No;
        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {
            if (departid == null) departid = "";
            if (departid.Length > 0)
            {
                SetEduDepartment F2 = new SetEduDepartment(departid);
                F2.ShowDialog(this);
                if (F2.DialogResult == System.Windows.Forms.DialogResult.OK)
                {
                    departid = F2.choose;

                }
                else
                {
                    departid = null;
                }
            }
            else
            {
                SetEduDepartment F2 = new SetEduDepartment(null);
                F2.ShowDialog(this);
                if (F2.DialogResult == System.Windows.Forms.DialogResult.OK)
                {
                    departid = F2.choose;

                }
                else
                {
                    departid = "";
                }
            }
        }
    }
}
